package  server.persistencia;

import server.dominio.DescripcionRol;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

 



public class DAODescripcionRolMySQL extends DAODescripcionRol {
	private Agente Broker;
	private Connection Con;
	
	public DAODescripcionRolMySQL() throws  SQLException{
		Broker= Agente.getAgente();
		Con= Broker.getConnection();
	}
	
	@Override
	public int delete(Object rol) throws SQLException  {
		// TODO Auto-generated method stub
		PreparedStatement stmt;
		int result= 0;
		
		stmt = Con.prepareStatement("DELETE FROM DescripcionRol WHERE descripcionRol= ?");
		stmt.setString(1, ((DescripcionRol)rol).getDescripcionRol());
		result= stmt.executeUpdate();
		

			
		return result;
	}

	@Override
	public int insert(Object obj) throws SQLException {
		DescripcionRol aux= (DescripcionRol)obj;
		int resul=0;
		
		PreparedStatement stmt = Con.prepareStatement("INSERT INTO DescripcionRol(descripcionRol) VALUES (?)");

		stmt.setString(1, aux.getDescripcionRol());
	
		resul= stmt.executeUpdate();
		
         stmt.close(); 
	    
		

		return resul;
	}

	public ArrayList<Object> select(String condicion) throws SQLException{
		ArrayList<Object> roles = new ArrayList<Object>();
		Statement stmt;
		
		stmt = Con.createStatement();
		ResultSet res  = stmt.executeQuery(condicion);
		 while(res.next()) 
         { 
			 DescripcionRol aux= new DescripcionRol(res.getInt("idrol_pk"), res.getString("descripcionRol"));
			 roles.add(aux);
         } 
		 res.close(); 
		 stmt.close(); 


		return roles;
	}

	@Override
	public int update(Object obj) {
		// TODO Auto-generated method stub
			return 1;
	}

	@Override
	public Object find(String condicion) throws SQLException {
		// TODO Auto-generated method stub
		DescripcionRol rol= null;
		Statement stmt;
	
		stmt = Con.createStatement();
		ResultSet res  = stmt.executeQuery(condicion);
		if(res.next())
			 rol= new DescripcionRol(res.getInt("idRol_pk"), res.getString("descripcionRol"));
		
        stmt.close();
	
		
		return rol;
	}

	public void cerrarDAO() throws SQLException{
		Con.close();
	}
}
